<?php
	/**
	 * !! PDO NOTICE !!
	 * END USERS RESPONSIBILITY TO CHECK FOR ERRORS
	 *  -> FULL STACK TRACE ON ERROR RESULTS IN SECURITY LEAK
	 */
	 
	class sql_mysql
	{
		/**
		 * PDO Object
		 */
		public $db = null;
		/**
		 * Core reference
		 */
		public $core = null;
		
		public function __construct( $config, $core )
		{
			$this->core = &$core;

			try
			{
				$this->db = new PDO('mysql:host='.$config['host'].';dbname='.$config['base'], $config['user'], $config['pass'], array(PDO::ATTR_PERSISTENT => true, PDO::ERRMODE_EXCEPTION => true));
			}
			catch(PDOException $e)
			{
				$this->core->general_error_handler($this->purify($e->getMessage(), $config));
			}
			catch(Exception $e)
			{
				$this->core->general_error_handler($this->purify($e->getMessage(), $config));
			}
		}
		
		/**
		 * Ensures the safety of database user and password
		 */
		private function purify($what, $config)
		{
			return strtr( $what, array( $config['user'] => '*censored*', $config['pass'] => '*censored*') );
		}
		
		/**
	 	 * @since Feb 12th, 2008
	 	 * @update Replaced $name(&$this->db) -> $name($this->db)
		 */  	 		
		public function getInterface( $name )
		{
			// Silence the E_WARNING given out
			if(!@include_once( './core/interfaces/'.$name.'.interface.php' ))
			{
				throw new Exception('NGen::SQL -> Failed to load SQL interface "'.$name.'"');
			}
			else
			{
				if($this->db !== null)
				{
					include_once( './core/interfaces/'.$name.'.interface.php' );
					return new $name($this->db);
				}
				
				// the previous method returns if db isn't null
				throw new Exception('NGen::SQL -> Cannot load interface with an unconstructed database object');
			}
		}
	}
?>